package hos.ui.drawable;

import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;


/**
 * <p>Title: DrawableUtils </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-06-26 11:03
 */
public class DrawableUtils {
    private DrawableUtils() {
    }

    public static StateListDrawable getDrawable(Drawable normal, Drawable state) {
        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(new int[]{android.R.attr.state_selected}, state);
        stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, state);
        stateListDrawable.addState(new int[]{android.R.attr.state_checked}, state);
        stateListDrawable.addState(new int[]{android.R.attr.state_focused}, state);
        stateListDrawable.addState(new int[]{android.R.attr.state_active}, state);
        stateListDrawable.addState(new int[]{android.R.attr.state_enabled}, normal);
        stateListDrawable.addState(new int[]{}, normal);
        return stateListDrawable;
    }

    public static GradientDrawable getBackground( int color) {
        return getBackground(color, 12f);
    }


    public static GradientDrawable getBackground( int color,
                                                 float radius) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(color);
        gradientDrawable.setCornerRadius(radius);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            gradientDrawable.setPadding(24, 6, 24, 6);
        } else {
            gradientDrawable.getPadding(new Rect(24, 6, 24, 6));
        }
        return gradientDrawable;
    }

    public static GradientDrawable getBackground( int color,
                                                 float topLeftRadius, float topRightRadius,
                                                 float bottomLeftRadius, float bottomRightRadius) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(color);
        gradientDrawable.setCornerRadii(new float[]{
                topLeftRadius, topLeftRadius,
                topRightRadius, topRightRadius,
                bottomRightRadius, bottomRightRadius,
                bottomLeftRadius, bottomLeftRadius
        });
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            gradientDrawable.setPadding(24, 6, 24, 6);
        } else {
            gradientDrawable.getPadding(new Rect(24, 6, 24, 6));
        }
        return gradientDrawable;
    }

    public static GradientDrawable getBackgroundStroke( int color,  int strokeColor) {
        return getBackgroundStroke(color, 2, strokeColor, 12f);
    }

    public static GradientDrawable getBackgroundStroke( int color, int width,  int strokeColor) {
        return getBackgroundStroke(color, width, strokeColor, 12f);
    }

    public static GradientDrawable getBackgroundStroke( int color, int width,  int strokeColor,
                                                       float radius) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setStroke(width, strokeColor);
        gradientDrawable.setColor(color);
        gradientDrawable.setCornerRadius(radius);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            gradientDrawable.setPadding(24, 6, 24, 6);
        } else {
            gradientDrawable.getPadding(new Rect(24, 6, 24, 6));
        }
        return gradientDrawable;
    }

    public static GradientDrawable getBackgroundStroke( int color, int width,  int strokeColor,
                                                       float topLeftRadius, float topRightRadius,
                                                       float bottomLeftRadius, float bottomRightRadius) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setStroke(width, strokeColor);
        gradientDrawable.setColor(color);
        gradientDrawable.setCornerRadii(new float[]{
                topLeftRadius, topLeftRadius,
                topRightRadius, topRightRadius,
                bottomRightRadius, bottomRightRadius,
                bottomLeftRadius, bottomLeftRadius
        });
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            gradientDrawable.setPadding(24, 6, 24, 6);
        } else {
            gradientDrawable.getPadding(new Rect(24, 6, 24, 6));
        }
        return gradientDrawable;
    }

    public static GradientDrawable getBorderRadius( int color) {
        return getBorderRadius( color,2, 12f);
    }

    public static GradientDrawable getBorderRadius(  int color,int width) {
        return getBorderRadius( color,width, 12f);
    }

    public static GradientDrawable getBorderRadius( int color,int width,
                                                   float radius) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setStroke(width, color);
        gradientDrawable.setCornerRadius(radius);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            gradientDrawable.setPadding(24, 6, 24, 6);
        } else {
            gradientDrawable.getPadding(new Rect(24, 6, 24, 6));
        }
        return gradientDrawable;
    }

    public static GradientDrawable getBorderRadius(  int color,int width,
                                                   float topLeftRadius, float topRightRadius,
                                                   float bottomLeftRadius, float bottomRightRadius) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setStroke(width, color);
        gradientDrawable.setCornerRadii(new float[]{
                topLeftRadius, topLeftRadius,
                topRightRadius, topRightRadius,
                bottomRightRadius, bottomRightRadius,
                bottomLeftRadius, bottomLeftRadius
        });
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            gradientDrawable.setPadding(24, 6, 24, 6);
        } else {
            gradientDrawable.getPadding(new Rect(24, 6, 24, 6));
        }
        return gradientDrawable;
    }

    public static Drawable copyGradientDrawable(Drawable drawable){
        Drawable.ConstantState constantState = drawable.getConstantState();
        if (constantState != null) {
            return constantState.newDrawable();
        }
        return drawable;
    }
}
